<template>
  <div class="app-container">
    <el-dialog title="添加" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="层次代码">
          <el-input v-model="form.itemCode"></el-input>
        </el-form-item>
        <el-form-item label="层次名称">
          <el-input v-model="form.itemName"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="编辑" :visible.sync="editFormVisible">
      <el-form :model="form">
        <el-form-item label="层次代码">
          <el-input v-model="form.itemCode"></el-input>
        </el-form-item>
        <el-form-item label="层次名称">
          <el-input v-model="form.itemName"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="editSubmit">确 定</el-button>
      </div>
    </el-dialog>
    <el-row type="flex" justify="end">
      <el-button type="success" @click="add">新建</el-button>
    </el-row>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column type="index" label="序号" align="center">
      </el-table-column>
      <el-table-column prop="itemCode" label="层次代码" align="center">
      </el-table-column>
      <el-table-column prop="itemName" label="层次名称" align="center">
      </el-table-column>
      <el-table-column
        prop="address"
        label="操作"
        width="180"
        align="center"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button type="success" @click="edit(scope.row)">编辑</el-button>
          <el-button type="danger" @click="del(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      layout="prev, pager, next"
      :total="total"
      :page-size="pageSize"
      :current-page="currentPage"
      @current-change="currentChange"
    >
    </el-pagination>
  </div>
</template>

<script>
import { getList, deleteData, addData, editData } from "@/api/Level";
export default {
  data() {
    return {
      tableData: [],
      total: 0,
      pageSize: 10,
      currentPage: 0,
      dialogFormVisible: false,
      editFormVisible: false,
      form: {},
      currentObj: {},
    };
  },
  methods: {
    indexMethod(index) {
      return index * 2;
    },
    submit() {
      this.dialogFormVisible = false;
      addData(this.form)
        .then((res) => {
          if (res === 1) {
            this.$message.success("添加成功");
          }
        })
        .catch((err) => {
          this.$message.error("添加失败");
        });
      this.form = {};
    },
    add() {
      this.dialogFormVisible = true;
    },
    edit(data) {
      this.editFormVisible = true;
      this.currentObj = data;
    },
    editSubmit() {
      this.editFormVisible = false;
      let data = {};
      data.itemCode = this.form.itemCode;
      data.itemName = this.form.itemName;
      data.id = this.currentObj.id;

      editData(data)
        .then((res) => {
          this.$message.success("添加成功");
          this.currentObj.itemCode = data.itemCode;
          this.currentObj.itemName = data.itemName;
        })
        .catch((err) => {
          this.$message.error("更新失败");
        });
      this.form = {};
    },
    del(data) {
      console.log(data);
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          deleteData(data)
            .then((res) => {
              this.$message.success("删除成功");
              this.tableData.forEach((e, index) => {
                if (e.id === data.id) {
                  this.tableData.splice(index, 1);
                }
              });
            })
            .catch((err) => {
              this.$message.error("删除失败");
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    currentChange(current) {
      let data = {
        pageSize: 10,
        pageNo: current,
      };
      getList(data).then((res) => {
        this.tableData = res.resultList;
      });
    },
  },
  mounted() {
    let data = {
      pageSize: 10,
      pageNo: 1,
    };
    getList(data).then((res) => {
      console.log(res);
      this.total = res.total;
      this.tableData = res.resultList;
    });
  },
};
</script>

<style>
</style>